<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<!-- Description and console is hidden until the test ends so that the content of the console cannot be searchable. -->
<p id="description" style="visibility: hidden;"></p>
<pre id="console" style="visibility: hidden;"></pre>
<script>
description("Find text in shadow roots.");

function testSimpleShadow()
{
    debug('Starting testSimpleShadow()...');
    var container = document.createElement('div');
    document.body.appendChild(container);
    container.innerHTML = 'BEFORE<span id="host">DOCUMENT</span>AFTER';
    document.getElementById('host').attachShadow({mode: 'open'}).innerHTML = '<span>SHADOW</span>';

    var selection = getSelection();
    selection.empty();
    shouldBeTrue('testRunner.findString("SHADOW", [])');
    selection.empty();
    shouldBeTrue('testRunner.findString("HADO", [])');
    selection.empty();
    shouldBeTrue('testRunner.findString("SHA", [])');
    selection.empty();
    shouldBeTrue('testRunner.findString("DOW", [])');
    selection.empty();
    shouldBeFalse('testRunner.findString("DOCUMENT", [])');
    selection.empty();
    shouldBeFalse('testRunner.findString("DOC", [])');
    selection.empty();
    shouldBeFalse('testRunner.findString("CUM", [])');
    selection.empty();
    shouldBeFalse('testRunner.findString("ENT", [])');
    selection.empty();
    shouldBeTrue('testRunner.findString("BEFORE", [])');
    selection.empty();
    shouldBeTrue('testRunner.findString("AFTER", [])');
    selection.empty();
    shouldBeFalse('testRunner.findString("BEFOREDOC", [])');
    selection.empty();
    shouldBeFalse('testRunner.findString("ENTAFTER", [])');
    selection.empty();
    shouldBeFalse('testRunner.findString("ORESHADOW", [])'); // FIXME: Should find text spanning over multiple trees.
    selection.empty();
    shouldBeFalse('testRunner.findString("SHADOWAFTER", [])');

    document.body.removeChild(container);
    debug('Finished testSimpleShadow().\n');
}

function testNestedShadows()
{
    debug('Starting testNestedShadows()...');
    var container = document.createElement('div');
    document.body.appendChild(container);
    container.innerHTML = '<span id="host">DOCUMENT</span>';
    var outerShadowRoot = document.getElementById('host').attachShadow({mode: 'open'});
    outerShadowRoot.innerHTML = '<span>OUTER<span id="host">SHADOW</span></span>';
    outerShadowRoot.getElementById('host').attachShadow({mode: 'open'}).innerHTML = '<span>INNER</span>';

    var selection = getSelection();
    selection.empty();
    shouldBeFalse('testRunner.findString("DOCUMENT", [])');
    selection.empty();
    shouldBeTrue('testRunner.findString("OUTER", [])');
    selection.empty();
    shouldBeFalse('testRunner.findString("SHADOW", [])');
    selection.empty();
    shouldBeTrue('testRunner.findString("INNER", [])');

    document.body.removeChild(container);
    debug('Finished testNestedShadows().\n');
}

function testDistribution()
{
    debug('Starting testDistribution()...');
    var container = document.createElement('div');
    document.body.appendChild(container);
    container.innerHTML = '<span id="host">DOCUMENT</span>';
    document.getElementById('host').attachShadow({mode: 'open'}).innerHTML = '<span>BEFORE<slot></slot>AFTER</span>';

    var selection = getSelection();
    selection.empty();
    shouldBeTrue('testRunner.findString("BEFORE", [])');
    selection.empty();
    shouldBeTrue('testRunner.findString("DOCUMENT", [])');
    selection.empty();
    shouldBeTrue('testRunner.findString("AFTER", [])');
    selection.empty();
    shouldBeTrue('testRunner.findString("BEFOREDOCUMENTAFTER", [])'); // Visited in flat tree order
    selection.empty();
    shouldBeFalse('testRunner.findString("DOCUMENTBEFORE", [])');
    selection.empty();
    shouldBeFalse('testRunner.findString("AFTERDOCUMENT", [])');

    document.body.removeChild(container);
    debug('Finished testDistribution().\n');
}

if (window.testRunner) {
    testSimpleShadow();
    testNestedShadows();
    testDistribution();
} else {
    testFailed('This test requires testRunner.');
}

// Post-test clean-up.
document.getElementById('description').style.removeProperty('visibility');
document.getElementById('console').style.removeProperty('visibility');
</script>
</body>
</html>
